﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;

namespace WebRole1
{
  public partial class Login : BasicPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ctx.MyClsStorageAccount.IsAccountValid)
        {
            Response.Redirect("default.aspx");
        }
        else
        {
            lblMessage.Text = "Storage Account information is not provided or is invalid";
        }

      mFill();
    }

    private void mFill()
    {
        panelDebug.Visible = false;

#if DEBUG
        panelDebug.Visible = true;
#endif

    }

    private void mSetDefaultValues()
    {
#if DEBUG
        txtStorageName.Text = clsStatic.getAppSettingsString("StorageName");
        txtStorageKey.Text = clsStatic.getAppSettingsString("StorageKey");
        txtDeploymentID.Text = clsStatic.getAppSettingsString("DeploymentID");
#endif
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        try
        {
            lblMessage.Text = "";
            DoLogin();
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            lblMessage.Text = ex.Message;
        }
    }

    private void DoLogin()
    {
        clsStorageAccount sa = ctx.MyClsStorageAccount;

        sa.StorageName = txtStorageName.Text;
        sa.StorageKey = txtStorageKey.Text;
        sa.DeploymentID = txtDeploymentID.Text;
        sa.Save();

        if (sa.IsAccountValid)
        {
            Response.Redirect("default.aspx", false);           
        }
        else
        {
            lblMessage.Text = "Cannot connect to the storage account. Please recheck the entered info";
        }
 
    }

    protected void btnSetDefaultValues_Click(object sender, EventArgs e)
    {
        try
        {
            mSetDefaultValues();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

  }
}